package cn.com.fuhuitong.main.access.ui.fragment

import cn.com.fuhuitong.base.BaseAbstractFragment
import com.jakewharton.rxbinding2.widget.RxTextView
import cn.com.fuhuitong.R
import io.reactivex.Observable
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable
import io.reactivex.functions.Function5
import io.reactivex.functions.Function6
import kotlinx.android.synthetic.main.fragment_access_wechat.*

class AccessWeChatFragment : BaseAbstractFragment() {

    private val dispose: CompositeDisposable = CompositeDisposable()

    companion object {
        fun newInstance(): AccessWeChatFragment {
            return AccessWeChatFragment()
        }
    }

    override val layoutResId: Int
        get() = R.layout.fragment_access_wechat

    override fun initView() {

        val observableInviteesPhone: Observable<CharSequence> =
            RxTextView.textChanges(edit_wechat_invitees_phone)
        val observableInviteesName: Observable<CharSequence> =
            RxTextView.textChanges(edit_wechat_invitees_name)

        val observableNickname: Observable<CharSequence> =
            RxTextView.textChanges(edit_wechat_nickname)
        val observableCard: Observable<CharSequence> =
            RxTextView.textChanges(edit_wechat_card)

        val observablePhone: Observable<CharSequence> =
            RxTextView.textChanges(edit_wechat_phone)
                .doOnNext {
                    text_wechat_code.isEnabled = it.isNotEmpty()
                }
        val observableCode: Observable<CharSequence> =
            RxTextView.textChanges(edit_wechat_code)

        val observable: Disposable =
            Observable.combineLatest(observableInviteesPhone,
                observableInviteesName,
                observableNickname,
                observableCard,
                observablePhone,
                observableCode,
                Function6<CharSequence, CharSequence, CharSequence, CharSequence, CharSequence, CharSequence, Boolean> { t1, t2, t3, t4, t5, t6 ->
                    t1.isNotEmpty() && t2.isNotEmpty() && t3.isNotEmpty() && t4.isNotEmpty() && t5.isNotEmpty() && t6.isNotEmpty()
                })
                .subscribe {
                    text_wechat_binding.isEnabled = it
                }
        dispose.add(observable)

    }

    override fun onDestroyView() {
        super.onDestroyView()
        if (dispose.size() > 0 && !dispose.isDisposed) {
            dispose.dispose()
        }
    }
}